//
// Created by dreamli on 2025/11/7.
//
#include <stdio.h>
#include "sqlite3.h"
#include "database.h"
#include "foods_category_control.h"
//插入数据
int foods_category_insert(){
    const char * sql="insert into foods_category (category_id,category_name) values (?,?);";
    sqlite3_stmt * stmt;
    int rc= sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"foods_category_insert预编译出错了：%s",sqlite3_errmsg(db));
        return -1;
    }

    const int category_id[7] = {1, 2, 3,4,5,6,7};
    const char *category_name[7] = {"烤串类", "海鲜类", "素菜类","主食类","烤内脏","特色小吃","酒水饮料"};
    for (int i = 0; i < 7; i++) {
        sqlite3_bind_int(stmt, 1, category_id[i]);
        sqlite3_bind_text(stmt, 2, category_name[i], -1, SQLITE_TRANSIENT);
        rc = sqlite3_step(stmt);
        if (rc != SQLITE_DONE) {
            fprintf(stderr, "第 %d 条插入失败：%s\n", i+1, sqlite3_errmsg(db));
            sqlite3_reset(stmt);  // 重置语句，继续下一次
            continue;
        }
        printf("第 %d 条插入成功：ID=%d，食物=%s\n",
               i+1, category_id[i], category_name[i]);
        sqlite3_reset(stmt);  // 重置语句，清除当前绑定
    }
    sqlite3_finalize(stmt);
    return 0;
}
//添加数据
int foods_category_add(foods_category * category){
    char sql[256];
    sprintf(sql,"insert into foods_category(category_name) values ('%s');",
            category->category_name);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品种类添加出错了:%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品种类添加成功!\n");
    return 0;
}

//更新数据
int foods_category_update(foods_category * pcategory){
    char sql[256];
    sprintf(sql,
            "update foods_category set category_name='%s'  where category_id = %d; ",
            pcategory->category_name);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品种类修改出错了 : %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品种类修改成功了!\n");
    return 0;
}

//删除数据
int foods_category_delete(int id){
    char sql[256];
    sprintf(sql,
            "delete from foods_category  where category_id = %d; ",
            id);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr," 食品种类删除出错了: %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品种类删除成功!\n");
    return 0;
}
int callback(void *data,int argc,char **argv,char **arg_col_name){
    (*(int *)data)++;
    static int row=0;
    if(row==0){
        printf("| %-10s | %-15s |\n", "分类ID", "分类名称");
        printf("------------------------------------------\n");
        row = 1;
    }
    for (int i = 0; i < argc; i++) {
        if (i == 0) {  // 分类ID（整数）
            char *id = argv[i] ? argv[i] : "NULL";
            printf("| %-10s ", id);
        } else if (i == 1) {  // 分类名称（中文）
            char *name = argv[i] ? argv[i] : "未知分类";
            printf("| %-15s |\n", name);
        }
    }
    return 0;
}

int foods_category_query(){
    char * errmsg=NULL;
    int rowcount=0;
    char * sql="select category_id,category_name from foods_category";
    int rc= sqlite3_exec(db,sql,callback,&rowcount,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品种类查询失败：%s",errmsg);
        return -1;
    }
    printf("食品种类查询成功！\n");
    return 0;
}
